package com.jixing.rengran.huaji.ui.activity;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.AppCompatEditText;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jixing.rengran.huaji.R;
import com.jixing.rengran.huaji.mvp.model.bean.HttpData;
import com.jixing.rengran.huaji.mvp.model.bean.UserBean;
import com.jixing.rengran.huaji.mvp.presenter.LoginPresenter;
import com.jixing.rengran.huaji.mvp.view.LoginView;
import com.jixing.rengran.huaji.ui.activity.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends BaseActivity<LoginView,LoginPresenter> implements LoginView,View.OnClickListener {
    //数据声明
    List<String> mTitles;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    setShowAnimation(loginTitleText, 2500);
                    break;
                case 1:
                    setHideAnimation(loginTitleText, 1500);
                    break;
                case 3:
                    loginTitleText.setText((String) msg.obj);
                    break;
                default:
                    break;
            }
        }

    };

    //组件实例化
    @BindView(R.id.login_title_text)
    TextView loginTitleText;
    @BindView(R.id.zuohuaji_image)
    ImageView zuohuajiImage;
    @BindView(R.id.youhuaji_image)
    ImageView youhuajiImage;
    @BindView(R.id.login_title)
    RelativeLayout loginTitle;
    @BindView(R.id.username_acet)
    AppCompatEditText usernameAcet;
    @BindView(R.id.username_til)
    TextInputLayout usernameTil;
    @BindView(R.id.password_acet)
    AppCompatEditText passwordAcet;
    @BindView(R.id.password_til)
    TextInputLayout passwordTil;
    @BindView(R.id.login_button)
    Button loginButton;
    @BindView(R.id.qqlogin_ll)
    LinearLayout qqloginLl;
    @BindView(R.id.weixin_ll)
    LinearLayout weixinLl;
    @BindView(R.id.weibologin_ll)
    LinearLayout weibologinLl;
    @BindView(R.id.reg_text)
    TextView regText;
    @BindView(R.id.content)
    LinearLayout content;
    @Override
    public int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void init() {
        setEnableTitle(false);
        mTitles = new ArrayList<>();
        mTitles.add("滑稽树上滑稽果");
        mTitles.add("滑稽树下你和我");
        mTitles.add("滑稽树前做游戏");
        mTitles.add("滑稽多又多");
        mTitles.add("欢迎使用滑稽树");
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        content.measure(w, h);
        TranslateAnimation animation = new TranslateAnimation(0, 0, content.getMeasuredHeight(), 0);
        animation.setDuration(500);//设置动画持续时间
        content.setAnimation(animation);
        youhuajiImage.setAnimation(animation);
        zuohuajiImage.setAnimation(animation);
        animation.start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < mTitles.size(); i++) {
                    try {
                        mHandler.sendEmptyMessage(1);
                        Thread.sleep(2500);
                        Message msg = new Message();
                        msg.what = 3;
                        msg.obj = mTitles.get(i);
                        mHandler.sendMessage(msg);
                        mHandler.sendEmptyMessage(0);
                        Thread.sleep(1500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();

        loginButton.setOnClickListener(this);
        passwordAcet.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    zuohuajiImage.setRotation(90);
                    youhuajiImage.setRotation(-90);
                } else {
                    zuohuajiImage.setRotation(0);
                    youhuajiImage.setRotation(0);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public String getTitleText() {
        return null;
    }

    @Override
    protected LoginPresenter getPresenter() {
        return new LoginPresenter();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_button:
                Verify();
                break;
            default:
                break;
        }
    }

    private void Verify() {
        usernameTil.setErrorEnabled(false);
        passwordTil.setErrorEnabled(false);
        if (usernameAcet.getText().length()<6){
            usernameTil.setErrorEnabled(true);
            usernameTil.setError("账号格式不正确");
        }else if (passwordAcet.getText().length()<6){
            passwordTil.setErrorEnabled(true);
            passwordTil.setError("密码格式不正确");
        }else{
            mPresenter.Login(String.valueOf(usernameAcet.getText()), String.valueOf(passwordAcet.getText()));
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void onError(Throwable t) {

    }

    @Override
    public void doLoginResult(HttpData<UserBean> data) {

    }
}
